package com.hjc.controller;

import com.hjc.controller.form.TestSayHelloForm;
import com.hjc.utils.FileUploadUtils;
import com.hjc.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

@RestController
@RequestMapping("/test")
@Api("测试Web接口")
public class TestController {

    @PostMapping("/sayHello")
    @ApiOperation("测试")
    public R sayHello(@Valid @RequestBody TestSayHelloForm form) {
        return R.ok().put("message", "Hello " + form.getName());
    }

    @PostMapping("/addUser")
    @ApiOperation("添加用户")
    @RequiresPermissions(value = {"ROOT", "USER_ADD"}, logical = Logical.OR)
    public R addUser() {
        return R.ok("添加成功");
    }


    /**
     * 图片上传
     */
    @PostMapping("/uploadImg")
    public R avatar(@RequestParam("img") MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            String filePath = FileUploadUtils.upload(file);
            return R.ok().put("path", filePath);
        }
        return R.error();
    }
}
